package com.ez.supervise.action.sys;

import java.util.List;

import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;

import org.apache.commons.beanutils.BeanUtils;

import com.ez.supervise.action.base.BaseJsonAction;
import com.ez.supervise.core.Page;
import com.ez.supervise.model.sys.Permission;
import com.ez.supervise.service.PermissionService;


public class PermissionAction extends BaseJsonAction {

	private static final long serialVersionUID = 8585792179848670015L;
	private Long id;
	private PermissionService permissionService;
	private Permission permission;
	private Page page;

	public void savePermission() {
		try {
			if (permission.isNew()) {
				Permission savePermission = new Permission();
				// savePermission.setName(permission.getName());
				// savePermission.setMemo(permission.getMemo());
				permissionService.insert(savePermission);
			} else {
				permissionService.update(permission);
			}
			outJsonString(RESULT_SUCCESS);
		} catch (Exception e) {
			outJsonString(RESULT_FAIL);
		}
	}

	public String findAllPermission() {
		page = new Page();
		page.setRoot(permissionService.findAll());
		page.setTotalProperty(page.getRoot().size());
		JsonConfig config = new JsonConfig();
		config.setExcludes(new String[] { "creator", "createTime", ",modifier",
				"modifyTime", "isDeleted", "new", });

		JSONObject jsonObj = JSONObject.fromObject(page, config);
		// System.out.println(jsonObj.toString());
		outJsonString(jsonObj.toString());
		return SUCCESS;
	}

	public String findListPermission() {
		List result = permissionService.findAll();
		JsonConfig config = new JsonConfig();
		config.setExcludes(new String[] { "creator", "createTime", ",modifier",
				"modifyTime", "isDeleted", "new", });
		JSONObject jsonObj = JSONObject.fromObject(result, config);
		outJsonString(jsonObj.toString());
		return SUCCESS;
	}

	/**
	 * @return
	 */
	public String deletePermission() {
		try {
			if (id != null && id > 0) {
				permissionService.delete(id);
			}
			outJsonString(RESULT_SUCCESS);
		} catch (Exception e) {
			outJsonString(RESULT_FAIL);
		}
		return SUCCESS;
	}

	/**
	 * @return
	 * @throws Exception
	 */
	public String updatePermission() throws Exception {
		String fieldName = getRequest().getParameter("fieldName");
		String fieldValue = getRequest().getParameter("fieldValue");
		String strPermissionId = getRequest().getParameter("id");
		try {
			if (id != null && id > 0) {
				Permission savePermission = new Permission();
				savePermission.setId(id);
				BeanUtils.setProperty(savePermission, fieldName, fieldValue);
				permissionService.update(savePermission);
			}
			outJsonString(RESULT_SUCCESS);
		} catch (Exception e) {
			outJsonString(RESULT_FAIL);
		}

		return SUCCESS;
	}

	public static void main(String[] args) {
		Permission permission = new Permission();
		permission.setId(1L);
		// permission.setName("linlinyu");
		JSONObject json = JSONObject.fromObject(permission);

		System.out.println(json);

		JsonConfig config = new JsonConfig();
		config.setExcludes(new String[] { "createTime", "modifyTime" });

		JSONObject jsonObj = JSONObject.fromObject(permission, config);
		System.out.println(jsonObj.toString());

	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public void setPermissionService(PermissionService permissionService) {
		this.permissionService = permissionService;
	}

	public PermissionService getPermissionService() {
		return permissionService;
	}

	public void setPermission(Permission permission) {
		this.permission = permission;
	}

	public Permission getPermission() {
		return permission;
	}
}
